package org.bite.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.bite.model.userInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {
    @RequestMapping("/param1")
    public String param1(String userName){
        System.out.println("接受参数："+userName);
        return "接受参数："+userName;
    }
    @RequestMapping("/param2")
    public Integer param2(Integer age){
        System.out.println("age:"+age);
        return age;
    }
    @RequestMapping("/param3")
    public int param3(int age){
        System.out.println("age:"+age);
        return age;
    }
    @RequestMapping("/param4")
    public String param4(String userName,String Password){
        return "userName:"+userName+",Password:"+Password;
    }
    @RequestMapping("/param5")
    public userInfo param5(userInfo user){
        System.out.println(user);
        return user;
    }
    //后端重命名
    @RequestMapping("/param6")
    public String param6(@RequestParam(value = "userName",required = false)String Name, @RequestParam(value = "password",required = false)String Pass){
        return "userName:"+Name+",Password:"+Pass;
    }
    @RequestMapping("/param7")
    public String param7(String[] arr){
        return Arrays.toString(arr);
    }
    //传递数组
    @RequestMapping("/param8")
    public String[] param8(String[] arr){
        return arr;
    }
    // 传递json
    @RequestMapping("/param9")
    public String param9(@RequestBody userInfo user){
         return "user:"+user;
    }
    //传递集合
    @RequestMapping("/param10")
    public String param10(@RequestParam("listParam") List<String> listParam){
        return "接受到:"+listParam;
    }
    //从URL中获取值
    @RequestMapping("/param11/{Id}")
    public String param11(@PathVariable("Id") Integer Id) {
        return "获取到值:" +Id;
    }
    //上传文件
    @RequestMapping("/param12")
    public String param12(@RequestPart("files") MultipartFile file) throws IOException {
//        System.out.println(file.getName());
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("F:/Img/"+file.getOriginalFilename()));
        return "接收到："+file.getOriginalFilename();
    }
    //获取cookie
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for(Cookie cookie : cookies){
                System.out.println(cookie.getName() + " : " + cookie.getValue());
            }
        }
        return "获取Cookie成功";
    }

    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("name")String name){
        System.out.println("Cookie name value: " + name);
        return "获取cookie 成功";
    }
    //设置session
    @RequestMapping("setSession")
    public String setSession(HttpServletRequest request){
        //有true 没有session会创建一个session
        HttpSession session = request.getSession(true);
        session.setAttribute("name","Tom");
        session.setAttribute("age","20");
        session.setAttribute("sex","man");
        session.setAttribute("height","128");
        session.setAttribute("weight","22");
        return "Session 设置成功";
    }
    @RequestMapping("/getSession        Cookie")
    public String getSession(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session != null){
            System.out.println(session.getAttribute("name"));
            System.out.println(session.getAttribute("sex"));
        }
        return "获取session成功1";
    }
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session){
        System.out.println(session.getAttribute("name"));
        System.out.println(session.getAttribute("sex"));
        return "获取session成功2";
    }
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute(value = "name",required = false) String name){
        System.out.println("name :" + name);
        return "获取session成功3";
    }

    //获取header

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String header = request.getHeader("User-Agent");
        System.out.println(header);
        return "获取到Headeer : "+header;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("Accept-Language") String header){
        System.out.println(header);
        return "获取到Headeer : "+header;
    }


}
